/*
 * Copyright (c) 2020-2021. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.depsea.oss.client;

import org.depsea.oss.exception.OssUploadException;
import org.depsea.oss.model.FileInfo;
import org.depsea.oss.strategy.FilenameHandleStrategy;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 文件上传
 *
 * @author Wang Chengwei
 * @since 1.0.0 2021/11/30 11:21
 */
public interface CustomerBucketOssUploadTemplate {

    /**
     * 上传文件，文件名为原始文件名
     *
     * @param bucketName Bucket Name
     * @param file 文件
     * @return 上传后的文件信息
     */
    default FileInfo upload(String bucketName, MultipartFile file) {
        return this.upload(bucketName, this.getFilenameHandleStrategy().getFilename(file.getOriginalFilename()), file);
    }

    /**
     * 上传文件
     *
     * @param bucketName Bucket Name
     * @param folder 文件名
     * @param file 文件
     * @return 上传后的文件信息
     */
    default FileInfo upload(String bucketName, String folder, MultipartFile file) {
        return this.upload(bucketName, folder, this.getFilenameHandleStrategy().getFilename(file.getOriginalFilename()), file);
    }

    /**
     * 上传
     *
     * @param bucketName Bucket Name
     * @param folder 文件夹
     * @param filename 文件名
     * @param file 文件
     * @return 上传后的文件信息
     */
    default FileInfo upload(String bucketName, String folder, String filename, MultipartFile file) {
        try {
            return this.upload(bucketName, folder, filename, file.getOriginalFilename(), file.getContentType(), file.getInputStream());
        } catch (IOException e) {
            throw new OssUploadException("文件上传失败", e);
        }
    }

    /**
     * 上传文件
     *
     * @param bucketName Bucket Name
     * @param originalFilename 原文件名
     * @param contentType 文件类型
     * @param ins 文件流
     * @return 上传后的文件信息
     */
    default FileInfo upload(String bucketName, String originalFilename, String contentType, InputStream ins) {
        return this.upload(bucketName, null, originalFilename, contentType, ins);
    }

    /**
     * 上传
     *
     * @param bucketName Bucket Name
     * @param folder 文件夹
     * @param originalFilename 原文件名
     * @param contentType 文件类型
     * @param ins 文件流
     * @return 上传后的文件信息
     */
    default FileInfo upload(String bucketName, String folder, String originalFilename, String contentType, InputStream ins) {
        return this.upload(bucketName, folder, this.getFilenameHandleStrategy().getFilename(originalFilename), originalFilename, contentType, ins);
    }

    /**
     * 上传
     *
     * @param bucketName Bucket Name
     * @param folder 文件夹
     * @param filename 文件名
     * @param originalFilename 原始文件名
     * @param contentType 文件类型
     * @param ins 文件流
     * @return 上传后的文件信息
     */
    FileInfo upload(String bucketName, String folder, String filename, String originalFilename, String contentType, InputStream ins);


    /**
     * 获取文件名处理策略
     *
     * @return 文件名处理策略
     */
    FilenameHandleStrategy getFilenameHandleStrategy();
}
